package com.springboot.controller;

import com.springboot.constant.ExamineExecl;
import com.springboot.constant.HealthRecordExecl;
import com.springboot.dto.ExamineDTO;
import com.springboot.enums.ErrorEnum;
import com.springboot.enums.ExamineStatus;
import com.springboot.exception.MyException;
import com.springboot.service.ExamineService;
import com.springboot.utils.Response;
import com.springboot.vo.ExamineVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @descrition:
 * @author: lzh
 */
@RestController
public class ExamineController {
    
    @Autowired
    ExamineService examineService;

    /**
     * @descrition:  小程序添加审核信息
     * @param examineDTO : 
     * @return : com.springboot.utils.Response
     */
    @PostMapping("/wx/examine")
    public Response addExamine(@Valid ExamineDTO examineDTO) {
        ExamineVO examineVO = examineService.addExamine(examineDTO);
        return Response.success(examineVO);
    }

    /**
     * @descrition:  小程序查询用户最近一条的审核信息
     * @param id :
     * @return : com.springboot.utils.Response
     */
    @GetMapping("/wx/examine/{id}")
    public Response findExamine(@PathVariable Integer id) {
        ExamineVO examineVO = examineService.findExamine(id);
        return Response.success(examineVO);
    }

    /**
     * @descrition:  后台获取审核列表
     * @param pageNum :
     * @param pageSize :
     * @param status :
     * @return : com.springboot.utils.Response
     */
    @GetMapping("/admin/examine")
    public Response getExamineList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                   @RequestParam(required = false) ExamineStatus status) {

        return Response.success(examineService.getExamineList(pageNum, pageSize, status));
    }

    /**
     * @descrition:  后台审核
     * @param id :
     * @param flag :
     * @return : com.springboot.utils.Response
     */
    @PostMapping("/admin/examine")
    public Response editExamine(@RequestParam Integer id, @RequestParam Integer flag) {
        examineService.editExamine(id, flag);
        return Response.success();
    }

    /**
     * @descrition:  获取审核execl表格
     * @param response :
     * @return : void
     */
    @GetMapping("/admin/examine/execl")
    public void dowloadExecl(HttpServletResponse response) {
        HSSFWorkbook wb = examineService.dowloadExecl();
        try {
            //设置Http响应头告诉浏览器下载这个附件
            response.setHeader("Content-Disposition"
                    , "attachment;filename=" +  URLEncoder.encode(ExamineExecl.SHEETNAME, "utf-8") + ".xls");
            OutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MyException(ErrorEnum.DOWLOAD_EXECL_ERROR);
        }
    }
}
